package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.minio.MinIoTemplate;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import com.heima.wemedia.thread.UserThreadLocal;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;


@Slf4j
@Service
@Transactional
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {


    @Autowired
    private WmMaterialMapper wmMaterialMapper;

    @Autowired
    private MinIoTemplate minIoTemplate;


    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) throws IOException {

        //判空
        String contentType = multipartFile.getContentType();
        if (StringUtils.isEmpty(contentType)) {
            log.warn("图片格式有误");
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_IMAGE_FORMAT_ERROR);
        }

        //1.将前端传过来的图片转换为流
        InputStream inputStream = multipartFile.getInputStream();
        //2.上传到minio中
        //2.1获取文件名,将参数中的contentType以/分割
        String[] fileSuffixNameArray= multipartFile.getContentType().split("/");
        String url = minIoTemplate.uploadFile("", System.currentTimeMillis() + "." + fileSuffixNameArray[1], inputStream, "image");

        //3.将保存此文件的路径保存到数据库中
        WmMaterial wmMaterial = new WmMaterial();
        //从ThreadLocal中将userId获取出来
        WmUser wmUser = UserThreadLocal.get();
        wmMaterial.setUserId(wmUser.getId());
        wmMaterial.setUrl(url);
        wmMaterial.setType((short)0);
        wmMaterial.setIsCollection((short)0);
        wmMaterial.setCreatedTime(new Date());

        wmMaterialMapper.insert(wmMaterial);
        return ResponseResult.okResult(wmMaterial);
    }

    @Override
    public ResponseResult findList(WmMaterialDto dto) {
       if (dto==null){
           return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
       }
       //当前页数
        Integer pageNum = dto.getPage();
       //每页多少条
        Integer size = dto.getSize();
        Page<WmMaterial> page = new Page<>(pageNum,size);
        //查询条件构造
        LambdaQueryWrapper<WmMaterial> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(dto.getIsCollection()==1,WmMaterial::getIsCollection,dto.getIsCollection());
        //分页查询
        Page<WmMaterial> wmMaterialPage = wmMaterialMapper.selectPage(page, wrapper);
        //获取需要返回的结果
        Integer total = Math.toIntExact(wmMaterialPage.getTotal());//总条数
        PageResponseResult result = new PageResponseResult(pageNum,size,total);
        result.setData(wmMaterialPage.getRecords());
        //返回结果
        return result;
    }
}
